﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class Goods
    {
        private readonly Chain.IDAL.Goods dal = new Chain.IDAL.Goods();

        public int Add(Chain.Model.Goods model)
        {
            if (this.Exists(model.GoodsCode))
            {
                return -1;
            }
            return this.dal.Add(model);
        }

        public int AddCustomField(string strGoodsCode, Hashtable customhash)
        {
            return this.dal.AddCustomField(strGoodsCode, customhash);
        }

        public List<Chain.Model.Goods> DataTableToList(DataTable dt)
        {
            List<Chain.Model.Goods> list = new List<Chain.Model.Goods>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.Goods item = new Chain.Model.Goods();
                    if ((dt.Rows[i]["GoodsID"] != null) && (dt.Rows[i]["GoodsID"].ToString() != ""))
                    {
                        item.GoodsID = int.Parse(dt.Rows[i]["GoodsID"].ToString());
                    }
                    if ((dt.Rows[i]["GoodsCode"] != null) && (dt.Rows[i]["GoodsCode"].ToString() != ""))
                    {
                        item.GoodsCode = dt.Rows[i]["GoodsCode"].ToString();
                    }
                    if ((dt.Rows[i]["GoodsClassID"] != null) && (dt.Rows[i]["GoodsClassID"].ToString() != ""))
                    {
                        item.GoodsClassID = int.Parse(dt.Rows[i]["GoodsClassID"].ToString());
                    }
                    if ((dt.Rows[i]["Name"] != null) && (dt.Rows[i]["Name"].ToString() != ""))
                    {
                        item.Name = dt.Rows[i]["Name"].ToString();
                    }
                    if ((dt.Rows[i]["NameCode"] != null) && (dt.Rows[i]["NameCode"].ToString() != ""))
                    {
                        item.NameCode = dt.Rows[i]["NameCode"].ToString();
                    }
                    if ((dt.Rows[i]["Unit"] != null) && (dt.Rows[i]["Unit"].ToString() != ""))
                    {
                        item.Unit = dt.Rows[i]["Unit"].ToString();
                    }
                    if ((dt.Rows[i]["GoodsNumber"] != null) && (dt.Rows[i]["GoodsNumber"].ToString() != ""))
                    {
                        item.GoodsNumber = int.Parse(dt.Rows[i]["GoodsNumber"].ToString());
                    }
                    if ((dt.Rows[i]["SalePercet"] != null) && (dt.Rows[i]["SalePercet"].ToString() != ""))
                    {
                        item.SalePercet = decimal.Parse(dt.Rows[i]["SalePercet"].ToString());
                    }
                    if ((dt.Rows[i]["GoodsSaleNumber"] != null) && (dt.Rows[i]["GoodsSaleNumber"].ToString() != ""))
                    {
                        item.GoodsSaleNumber = int.Parse(dt.Rows[i]["GoodsSaleNumber"].ToString());
                    }
                    if ((dt.Rows[i]["Price"] != null) && (dt.Rows[i]["Price"].ToString() != ""))
                    {
                        item.Price = decimal.Parse(dt.Rows[i]["Price"].ToString());
                    }
                    if ((dt.Rows[i]["CommissionType"] != null) && (dt.Rows[i]["CommissionType"].ToString() != ""))
                    {
                        item.CommissionType = int.Parse(dt.Rows[i]["CommissionType"].ToString());
                    }
                    if ((dt.Rows[i]["CommissionNumber"] != null) && (dt.Rows[i]["CommissionNumber"].ToString() != ""))
                    {
                        item.CommissionNumber = decimal.Parse(dt.Rows[i]["CommissionNumber"].ToString());
                    }
                    if ((dt.Rows[i]["Point"] != null) && (dt.Rows[i]["Point"].ToString() != ""))
                    {
                        item.Point = int.Parse(dt.Rows[i]["Point"].ToString());
                    }
                    if ((dt.Rows[i]["MinPercent"] != null) && (dt.Rows[i]["MinPercent"].ToString() != ""))
                    {
                        item.MinPercent = decimal.Parse(dt.Rows[i]["MinPercent"].ToString());
                    }
                    if ((dt.Rows[i]["GoodsType"] != null) && (dt.Rows[i]["GoodsType"].ToString() != ""))
                    {
                        item.GoodsType = int.Parse(dt.Rows[i]["GoodsType"].ToString());
                    }
                    if ((dt.Rows[i]["GoodsBidPrice"] != null) && (dt.Rows[i]["GoodsBidPrice"].ToString() != ""))
                    {
                        item.GoodsBidPrice = decimal.Parse(dt.Rows[i]["GoodsBidPrice"].ToString());
                    }
                    if ((dt.Rows[i]["GoodsRemark"] != null) && (dt.Rows[i]["GoodsRemark"].ToString() != ""))
                    {
                        item.GoodsRemark = dt.Rows[i]["GoodsRemark"].ToString();
                    }
                    if ((dt.Rows[i]["GoodsPicture"] != null) && (dt.Rows[i]["GoodsPicture"].ToString() != ""))
                    {
                        item.GoodsPicture = dt.Rows[i]["GoodsPicture"].ToString();
                    }
                    if ((dt.Rows[i]["GoodsCreateTime"] != null) && (dt.Rows[i]["GoodsCreateTime"].ToString() != ""))
                    {
                        item.GoodsCreateTime = DateTime.Parse(dt.Rows[i]["GoodsCreateTime"].ToString());
                    }
                    if ((dt.Rows[i]["CreateShopID"] != null) && (dt.Rows[i]["CreateShopID"].ToString() != ""))
                    {
                        item.CreateShopID = int.Parse(dt.Rows[i]["CreateShopID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int GoodsID)
        {
            return this.dal.Delete(GoodsID);
        }

        public bool DeleteList(string GoodsIDlist)
        {
            return this.dal.DeleteList(GoodsIDlist);
        }

        public bool ExeclDataInput(ArrayList sqlArray)
        {
            return this.dal.ExeclDataInput(sqlArray);
        }

        public bool Exists(int GoodsID)
        {
            return this.dal.Exists(GoodsID);
        }

        public bool Exists(string strGoodsCode)
        {
            return this.dal.Exists(strGoodsCode);
        }

        public bool Exists(int goodsID, string goodsCode)
        {
            return this.dal.Exists(goodsID, goodsCode);
        }

        public bool Exists(string strGoodsCode, int ShopID)
        {
            return this.dal.Exists(strGoodsCode, ShopID);
        }

        public bool Exists(int goodsID, string goodsCode, int ShopID)
        {
            return this.dal.Exists(goodsID, goodsCode, ShopID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetGoodLists(int PageSize, int PageIndex, out int resCount, string strTime, params string[] strWhere)
        {
            return this.dal.GetGoodLists(PageSize, PageIndex, out resCount, strTime, strWhere);
        }

        public DataSet GetGoodsExpense(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetGoodsExpense(PageSize, PageIndex, out resCount, strWhere);
        }

        public int GetGoodsID(string GoodsAccount)
        {
            return this.dal.GetGoodsID(GoodsAccount);
        }

        public DataSet GetGoodsInfo(string goodscode, int levelid)
        {
            return this.dal.GetGoodsInfo(goodscode, levelid);
        }

        public DataSet GetGoodsList(int PageSiza, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetGoodsList(PageSiza, PageIndex, out resCount, strWhere);
        }

        public DataSet GetGoodsListByMember(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetGoodsListByMember(PageSize, PageIndex, out resCount, strWhere);
        }

        public DataSet GetGoodsListByNoMember(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetGoodsListByNoMember(PageSize, PageIndex, out resCount, strWhere);
        }

        public DataSet GetGoodsListByShopID(int PageSize, int PageIndex, int ShopID, out int resCount, params string[] strWhere)
        {
            return this.dal.GetGoodsListByShopID(PageSize, PageIndex, ShopID, out resCount, strWhere);
        }

        public DataSet GetGoodsStockList(int ShopId, int MemLevelId, string GoodsIdList, int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            DataSet set = this.dal.GetGoodsStockList(ShopId, MemLevelId, GoodsIdList, PageSize, PageIndex, out resCount, strWhere);
            DataTable table = set.Tables[0];
            table.Columns.Add("GoodsDiscount");
            table.Columns.Add("PointDiscount");
            table.Columns.Add("DiscountPrice");
            table.Columns.Add("DiscountPoint");
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    decimal num = 1M;
                    decimal num2 = Convert.ToDecimal(row["minPercent"]);
                    decimal num3 = Convert.ToDecimal(row["salePercet"]);
                    if ((MemLevelId == -1) && (num3 > 0M))
                    {
                        num = num3;
                    }
                    if (MemLevelId != -1)
                    {
                        num = Convert.ToDecimal(row["ClassDiscountPercent"]);
                        if (num3 > 0M)
                        {
                            num = (num > num3) ? num3 : num;
                        }
                        if (num2 > 0M)
                        {
                            num = (num > num2) ? num : num2;
                        }
                    }
                    row["GoodsDiscount"] = num;
                    row["DiscountPrice"] = decimal.Round(Convert.ToDecimal(row["Price"]) * num, 2);
                    decimal num4 = 0M;
                    decimal num5 = Convert.ToDecimal(row["point"]);
                    if (MemLevelId != -1)
                    {
                        //switch (num5)
                        //{
                           // case -1M:
                                num4 = 0M;
                              //  break;

                           // case 0M:
                             //   num4 = Convert.ToDecimal(row["ClassPointPercent"]);
                               // break;
                      //  }
                    }
                    row["PointDiscount"] = num4;
                    if (num5 > 0M)
                    {
                        row["DiscountPoint"] = num5;
                    }
                    else if (num4 == 0M)
                    {
                        row["DiscountPoint"] = 0;
                    }
                    else
                    {
                        row["DiscountPoint"] = Math.Floor((decimal) (Convert.ToDecimal(row["DiscountPrice"]) / num4));
                    }
                    row["Price"] = decimal.Round(Convert.ToDecimal(row["Price"]), 2);
                    if (MemLevelId == -1)
                    {
                        row["ClassDiscountPercent"] = 1;
                        row["ClassPointPercent"] = 0;
                    }
                }
            }
            return set;
        }

        public DataSet GetItemAll(int intGoodsID)
        {
            return this.dal.GetItemAll(intGoodsID);
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetList(int PageSiza, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetList(PageSiza, PageIndex, out resCount, strWhere);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListSP(PageSize, PageIndex, out resCount, strWhere);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.Goods GetModel(int GoodsID)
        {
            return this.dal.GetModel(GoodsID);
        }

        public Chain.Model.Goods GetModel(string GoodsCode)
        {
            return this.dal.GetModel(GoodsCode);
        }

        public List<Chain.Model.Goods> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public DataSet GetStockRemind(string strWhere)
        {
            return this.dal.GetStockRemind(strWhere);
        }

        public DataSet GetStockRemind(string strWhere, int count)
        {
            return this.dal.GetStockRemind(strWhere, count);
        }

        public int Update(Chain.Model.Goods model)
        {
            if (!this.dal.Update(model))
            {
                return -3;
            }
            return 1;
        }
    }
}

